﻿using Infragistics.Controls.Editors;
using Infragistics.Controls.Editors.Primitives;
using Infragistics.Windows;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VNet.VisiSuite.Common.Logger;
using VNet.VisiSuite.Controls;
using VNet.VisiSuite.VisiCentral.Common;
using VNet.VisiSuite.VisiCentral.Common.Helpers;
using VNet.VisiSuite.VisiCentral.Controls;
using VNet.VisiSuite.VisiCentral.ViewModels;

namespace VNet.VisiSuite.VisiCentral.Views
{
    /// <summary>
    /// Interaction logic for AnalysisView.xaml
    /// </summary>
    public partial class AnalysisView : VisiPage
    {
        /// <summary>
        /// Store the current window element, used to restore the Rusco icon when the user control is unloaded
        /// </summary>
        private Window _currentWindow;

        public AnalysisView()
        {
            InitializeComponent();

            // Attached properties to storyboards
            _rightPanelExpanded = (Storyboard)this.FindResource("RightPanelExpanded");
            _rightPanelCollapsed = (Storyboard)this.FindResource("RightPanelCollapsed");
            Storyboard.SetTargetProperty(_rightPanelExpanded, new PropertyPath("Width"));
            Storyboard.SetTargetProperty(_rightPanelCollapsed, new PropertyPath("Width"));
        }

        #region Override Properties

        public override IList<VNetMenuItem> BottomMenuItems()
        {
            var viewModel = DataContext as AnalysisViewModel;
            if (viewModel == null) return null;

            return viewModel.BottomMenuItems;
        }
        #endregion

        #region Collapsible Panel

        private bool _isExpandedRightPanel = true;
        private Storyboard _rightPanelExpanded;
        private Storyboard _rightPanelCollapsed;

        private void RightPanelToggleButtonClick(object sender, RoutedEventArgs e)
        {
            if (_isExpandedRightPanel)
                _rightPanelCollapsed.Begin();
            else
                _rightPanelExpanded.Begin();

            _isExpandedRightPanel = !_isExpandedRightPanel;
        }

        #endregion

        #region Check boxes events

        /// <summary>
        /// Handles the Click event of the selectAllReasonCodes control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void selectAllReasonCodes_Click(object sender, RoutedEventArgs e)
        {
            if (reasonCodesTree != null)
            {
                CheckBox selectAllReasonCodes = (CheckBox)sender;
                // When check all is clicked, remove the event handler from the xam data tree to make sure 
                // the event NodeCheckedChanged won't fire continuously and make multiple requests to call back to
                // update the charts' data. Multiple requests could flood the web service.
                reasonCodesTree.NodeCheckedChanged -= reasonCodesTree_NodeCheckedChanged;
                if (selectAllReasonCodes.IsChecked.HasValue)
                {
                    if (selectAllReasonCodes.IsChecked.Value)
                    {
                        SelectAll((List<CheckedName>)reasonCodesTree.ItemsSource, true);
                    }
                    else
                    {
                        SelectAll((List<CheckedName>)reasonCodesTree.ItemsSource, false);
                    }
                }
                else
                {
                    SelectAll((List<CheckedName>)reasonCodesTree.ItemsSource, false);
                }
                // Restore the event handler for NodeCheckedChanged
                reasonCodesTree.NodeCheckedChanged += reasonCodesTree_NodeCheckedChanged;
            }
        }

        /// <summary>
        /// Handles the Click event of the selectAllCells control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void selectAllCells_Click(object sender, RoutedEventArgs e)
        {
            if (cellsTree != null)
            {
                CheckBox selectAllCells = (CheckBox)sender;
                // When check all is clicked, remove the event handler from the xam data tree to make sure 
                // the event NodeCheckedChanged won't fire continuously and make multiple requests to callback to
                // update the charts' data. Multiple requests could flood the web service.
                cellsTree.NodeCheckedChanged -= cellsTree_NodeCheckedChanged;

                if (selectAllCells.IsChecked.HasValue)
                {
                    if (selectAllCells.IsChecked.Value)
                    {
                        SelectAll((List<CheckedName>)cellsTree.ItemsSource, true);
                    }
                    else
                    {
                        SelectAll((List<CheckedName>)cellsTree.ItemsSource, false);
                    }
                }
                else
                {
                    SelectAll((List<CheckedName>)cellsTree.ItemsSource, false);
                }

                // Restore the event handler for NodeCheckedChanged
                cellsTree.NodeCheckedChanged += cellsTree_NodeCheckedChanged;
            }
        }

        /// <summary>
        /// Handles the Click event of the selectAllProducts control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void selectAllProducts_Click(object sender, RoutedEventArgs e)
        {
            if (productsTree != null)
            {
                CheckBox selectAllProducts = (CheckBox)sender;

                // When check all is clicked, remove the event handler from the xam data tree to make sure 
                // the event NodeCheckedChanged won't fire continuously and make multiple requests to callback to
                // update the charts' data. Multiple requests could flood the web service.
                productsTree.NodeCheckedChanged -= productsTree_NodeCheckedChanged;

                if (selectAllProducts.IsChecked.HasValue)
                {
                    if (selectAllProducts.IsChecked.Value)
                    {
                        SelectAll((List<CheckedName>)productsTree.ItemsSource, true);
                    }
                    else
                    {
                        SelectAll((List<CheckedName>)productsTree.ItemsSource, false);
                    }
                }
                else
                {
                    SelectAll((List<CheckedName>)productsTree.ItemsSource, false);
                }

                // Restore the event handler for NodeCheckedChanged
                productsTree.NodeCheckedChanged += productsTree_NodeCheckedChanged;
            }
        }

        /// <summary>
        /// Handles the Click event of the selectAllShifts control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void selectAllShifts_Click(object sender, RoutedEventArgs e)
        {
            if (shiftsTree != null)
            {
                // When check all is clicked, remove the event handler from the xam data tree to make sure 
                // the event NodeCheckedChanged won't fire continuously and make multiple requests to callback to
                // update the charts' data. Multiple requests could flood the web service.
                shiftsTree.NodeCheckedChanged -= shiftsTree_NodeCheckedChanged;

                CheckBox selectAllShifts = (CheckBox)sender;
                if (selectAllShifts.IsChecked.HasValue)
                {
                    if (selectAllShifts.IsChecked.Value)
                    {
                        SelectAll((List<CheckedName>)shiftsTree.ItemsSource, true);
                    }
                    else
                    {
                        SelectAll((List<CheckedName>)shiftsTree.ItemsSource, false);
                    }
                }
                else
                {
                    SelectAll((List<CheckedName>)shiftsTree.ItemsSource, false);
                }
                // Restore the event handler for NodeCheckedChanged
                shiftsTree.NodeCheckedChanged += shiftsTree_NodeCheckedChanged;
            }
        }

        private void SelectAll(List<CheckedName> items, bool? checkme)
        {
            if (checkme.HasValue)
            {
                if (items != null && items.Count > 0)
                {
                    foreach (var check in items)
                    {
                        check.IsChecked = (bool)checkme;
                    }

                    // Fire callback to update chart data
                    var viewModel = DataContext as AnalysisViewModel;
                    if (viewModel != null)
                        viewModel.UpdateChartData();
                }
            }
        }

        #endregion

        #region XamDataTree Events

        /// <summary>
        /// Handles the NodeCheckedChanged event of the reasonCodesTree control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Infragistics.Controls.Menus.NodeEventArgs"/> instance containing the event data.</param>
        private void reasonCodesTree_NodeCheckedChanged(object sender, Infragistics.Controls.Menus.NodeEventArgs e)
        {
            // Set the select all check box based on status of check boxes in the tree
            var viewModel = DataContext as AnalysisViewModel;
            if (viewModel != null && viewModel.ReasonCodes != null)
            {
                if (reasonCodesTree.Nodes.Count(t => t.IsChecked.HasValue && t.IsChecked.Value) == viewModel.ReasonCodes.Count)
                    selectAllReasonCodes.IsChecked = true;
                else
                    selectAllReasonCodes.IsChecked = false;

                // Fire callback in view model to update chart data
                viewModel.UpdateChartData();
            }
        }

        /// <summary>
        /// Handles the NodeCheckedChanged event of the cellsTree control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Infragistics.Controls.Menus.NodeEventArgs"/> instance containing the event data.</param>
        private void cellsTree_NodeCheckedChanged(object sender, Infragistics.Controls.Menus.NodeEventArgs e)
        {
            // Set the select all check box based on status of check boxes in the tree
            var viewModel = DataContext as AnalysisViewModel;
            if (viewModel != null && viewModel.Cells != null)
            {
                if (cellsTree.Nodes.Count(t => t.IsChecked.HasValue && t.IsChecked.Value) == viewModel.Cells.Count)
                    selectAllCells.IsChecked = true;
                else
                    selectAllCells.IsChecked = false;

                // Fire callback in view model to update chart data
                viewModel.UpdateChartData();
            }
        }

        /// <summary>
        /// Handles the NodeCheckedChanged event of the productsTree control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Infragistics.Controls.Menus.NodeEventArgs"/> instance containing the event data.</param>
        private void productsTree_NodeCheckedChanged(object sender, Infragistics.Controls.Menus.NodeEventArgs e)
        {
            // Set the select all check box based on status of check boxes in the tree
            var viewModel = DataContext as AnalysisViewModel;
            if (viewModel != null && viewModel.Products != null)
            {
                if (productsTree.Nodes.Count(t => t.IsChecked.HasValue && t.IsChecked.Value) == viewModel.Products.Count)
                    selectAllProducts.IsChecked = true;
                else
                    selectAllProducts.IsChecked = false;

                // Fire callback in view model to update chart data
                viewModel.UpdateChartData();
            }
        }

        /// <summary>
        /// Handles the NodeCheckedChanged event of the shiftsTree control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Infragistics.Controls.Menus.NodeEventArgs"/> instance containing the event data.</param>
        private void shiftsTree_NodeCheckedChanged(object sender, Infragistics.Controls.Menus.NodeEventArgs e)
        {
            // Set the select all check box based on status of check boxes in the tree
            var viewModel = DataContext as AnalysisViewModel;
            if (viewModel != null && viewModel.Shifts != null)
            {
                if (shiftsTree.Nodes.Count(t => t.IsChecked.HasValue && t.IsChecked.Value) == viewModel.Shifts.Count)
                    selectAllShifts.IsChecked = true;
                else
                    selectAllShifts.IsChecked = false;

                // Fire callback in view model to update chart data
                viewModel.UpdateChartData();
            }
        }

        #endregion

        #region Events

        /// <summary>
        /// Handles the Loaded event of the VisiPage control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void VisiPage_Loaded(object sender, RoutedEventArgs e)
        {
            // Remove the Rusco icon by setting a blank icon
            var parentWindow = Window.GetWindow(this);
            var iconUri = new Uri("pack://application:,,,/Assets/Icons/Blank.ico");
            parentWindow.Icon = BitmapFrame.Create(iconUri);
            _currentWindow = parentWindow;
        }

        /// <summary>
        /// Handles the Unloaded event of the VisiPage control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void VisiPage_Unloaded(object sender, RoutedEventArgs e)
        {
            // Restore the Rusco icon
            var iconUri = new Uri("pack://application:,,,/Assets/Icons/WorkRite.ico");
            if (_currentWindow != null)
                _currentWindow.Icon = BitmapFrame.Create(iconUri); ;
        }

        #endregion
    }
}
